<template>
  <div class="login">
    <div>
      <el-card class="box-card">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
        >
          <h3 class="title">欢迎使用煋玥科技</h3>
          <el-form-item prop="name">
            <el-input
              v-model="loginForm.name"
              type="text"
              auto-complete="off"
              placeholder="账号"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="passWord">
            <el-input
              v-model="loginForm.passWord"
              type="passWord"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="code">
            <el-select  placeholder="请选择"  v-model="loginForm.type">
              <el-option label="游客登录" value="1"></el-option>
              <el-option label="客服登录" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-checkbox
            v-model="loginForm.rememberMe"
            style="margin: 0px 0px 25px 0px"
            >记住密码</el-checkbox
          >
          <el-form-item style="width: 100%">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width: 100%"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div style="float: right" v-if="register">
              <router-link class="link-type" :to="'/register'"
                >立即注册</router-link
              >
            </div>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import Cookies from "js-cookie";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        name: "admin",
        passWord: "123456",
        rememberMe: false,
        type: 0,//0客服 1graph 2culture 3blog 
        src: "",
      },
      loginRules: {
        name: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        passWord: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        type: [
          { required: true, trigger: "blur", message: "请输入您的登录方式" },
        ],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  created() {
    this.getCookie();
    var user=Cookies.get("user")
    if (user === "" || user === null || user === undefined) {
      user=Cookies.getCookie("kefu")
      if (user === "" || user === null || user === undefined) {}
      this.$route.push("/home")
    }
  },
  methods: {
    getCookie() {
      const name = Cookies.get("name");
      const passWord = Cookies.get("passWord");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        name: name === undefined ? this.loginForm.name : name,
        passWord:
          passWord === undefined ? this.loginForm.passWord : decrypt(passWord),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("name", this.loginForm.name, { expires: 30 });
            Cookies.set("passWord", encrypt(this.loginForm.passWord), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("name");
            Cookies.remove("passWord");
            Cookies.remove("rememberMe");
          }
          this.$http.post("/login/login", this.loginForm).then((res) => {
            if(res.data.code===200){
              this.$message.success("登录成功")
              if(res.data.data.type==="0"){
                Cookies.set("kefu",JSON.stringify(res.data.data.user))
                this.$router.push("/kefu")
              }else{
                Cookies.set("user-Token",res.data.data.blog_token)

                this.loading = false;
                console.log(res)
              }         
            }else{
              this.$message.error(res.data.msg)
            }
          
          });

        }
      });
    },
  },
};
</script>

<style scoped>
.bg {
  background-image: url("../src/assets/images/login-bg.029b6041.png");
  background-size: contain;
  background-repeat: no-repeat;
}

.login {
  margin-top: 150px;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;

  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;

  font-size: 24px;
  color: #333;
  text-align: center;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }
}
.input-icon {
  height: 39px;
  width: 14px;
  margin-left: 2px;
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;
}
img {
  cursor: pointer;
  vertical-align: middle;
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
